package api

import (
	"github.com/astaxie/beego"
	"goblog/models/dao"
	"goblog/models"
)

type UserController struct {
	beego.Controller
	userDao *dao.UserDao
}

func (this *UserController) Post() {
	username := this.GetString("username")
	password := this.GetString("password")
	if this.userDao == nil {
		this.userDao = new(dao.UserDao)
	}
	user := this.userDao.GetByName(username)
	if &user == nil || user.Password != password {
		this.Data["json"] = models.JsonError("帐户密码错误")
	} else {
		this.Data["json"] = models.JsonSuccess("", user)
	}
	this.ServeJSON()
}

func (this *UserController) Login() {
	username := this.GetString("username")
	password := this.GetString("password")
	if this.userDao == nil {
		this.userDao = new(dao.UserDao)
	}
	user := this.userDao.GetByName(username)
	if user == nil || user.Password != password {
		this.Data["json"] = models.JsonError("帐户密码错误")
	} else {
		this.Data["json"] = models.JsonSuccess("", user)
	}
	this.ServeJSON()
}
